//
//  SEGNewFeedebackViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/1.
//

#import "SEGNewFeedebackViewModel.h"

@implementation SEGNewFeedebackViewModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        _dataSource = @[@{@"hasSelected":@"1",@"title":@"程序问题",@"type":@"1010"},
                        @{@"hasSelected":@"0",@"title":@"体验感受",@"type":@"1020"},
                        @{@"hasSelected":@"0",@"title":@"功能建议",@"type":@"1000"}];
    }
    return self;
}
#pragma mark - business
-(void)changeSelectedSource:(NSInteger)index{
    __block NSMutableArray *arrTemp = [NSMutableArray array];
    [self.dataSource enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:object];
        [tmp setValue:(index == idx) ? @"1":@"0" forKey:@"hasSelected"];
        [arrTemp addObject:tmp];
    }];
    self.dataSource = [NSArray arrayWithArray:arrTemp];
}
-(void)requestDictionaryInfoByCode:(void(^)(BOOL isSuccessed,NSString * content))completed{
    [[SEGFindDictionaryInfoManagr sharedManager] requestDictionaryInfoByCode:@"service_qq" completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        
        if (isSuccessed) {
            NSArray* arr = [[SEGFindDictionaryInfoManagr sharedManager].dataDictionary.result.firstObject.value componentsSeparatedByString:@","];
            
            if (arr.count > 0 && arr.count % 2 != 0) {
                NSMutableArray* arrM = [NSMutableArray arrayWithArray:arr];
                [arrM addObject:@" "];
                arr = [arrM copy];
            }
            
            if (arr.count <= 0) {
                completed(NO,nil);
                return;
            }
            NSString* content = [NSString stringWithFormat:@"如果您在使用%@时遇到问题或者有好的建议，\n欢迎加入我们的用户反馈QQ群：\n",kAppName];
            
            //判断奇偶数
            if (arr.count % 2 == 0) {
                for (NSInteger i = 0; i < arr.count; i = i + 2) {
                    NSString* str = [NSString stringWithFormat:@"%@         %@\n", arr[i], arr[i + 1]];
                    content = [content stringByAppendingString:str];
                }
            }
            completed(YES,content);
            return;
        }
        completed(NO,nil);
    }];
}

-(void)requestSuggestsaveWithContext:(NSString*)context SugType:(NSString*)type completed:(void(^)(NSString * codeType, NSString *errorMessage))completed{
    [[SEGSuggestsaveManager sharedManager] requestSuggestsaveWithContext:context sugType:type finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        completed (codeType,errorMessage);
    }];

}
#pragma mark - getter
@end
